home *** CD-ROM | disk | FTP | other *** search
/ Software 2000 / Software 2000 Volume 1 (Disc 1 of 2).iso / utilities / u264.dms / in.adf / ARPPRO3.0 / PRO.RUN / cliparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-18  |  2.1 KB  |  89 lines

  1. /* Copyright (C) 1986,1987 by Manx Software Systems, Inc. */
  2.  
  3. /*
  4.  *    This routine is called from the _main() routine and is used to
  5.  *    parse the arguments passed from the CLI to the program. It sets
  6.  *    up an array of pointers to arguments in the global variables and
  7.  *    and sets up _argc and _argv which will be passed by _main() to
  8.  *    the main() procedure. If no arguments are ever going to be
  9.  *    parsed, this routine may be replaced by a stub routine to reduce
  10.  *    program size.
  11.  *
  12.  *    If _arg_lin is non-zero, the _exit() routine will call FreeMem()
  13.  *    with _arg_lin as the memory to free and _arg_len as the size.
  14.  *
  15.  */
  16.  
  17. #include <libraries/dosextens.h>
  18.  
  19. extern int _argc;
  20. extern char **_argv;
  21. extern char *_detach_name;            /* for DETACHED programs */
  22.  
  23. _cli_parse(pp, alen, aptr)
  24. struct Process *pp;
  25. long alen;
  26. register char *aptr;
  27. {
  28.     register char *cp;
  29.     register struct CommandLineInterface *cli;
  30.     register int c;
  31.     void *ArpAlloc();
  32.     int _arg_len;
  33.     char *_arg_lin;
  34.  
  35.     if (pp->pr_CLI) {
  36.         cli = (struct CommandLineInterface *) ((long)pp->pr_CLI << 2);
  37.         cp = (char *)((long)cli->cli_CommandName << 2);
  38.     }
  39.     else
  40.         cp = _detach_name;
  41.     _arg_len = cp[0]+alen+2;
  42.     if ((_arg_lin = ArpAlloc((long)_arg_len)) == 0) ArpExit(20L,ERROR_NO_FREE_STORE);
  43.  
  44.     c = cp[0];
  45.     strncpy(_arg_lin, cp+1, c);
  46.     strcpy(_arg_lin+c, " ");
  47.     strncat(_arg_lin, aptr, (int)alen);
  48.     _arg_lin[c] = 0;
  49.     for (_argc=1,aptr=cp=_arg_lin+c+1;;_argc++) {
  50.         while ((c=*cp) == ' ' || c == '\t' || c == '\f' ||
  51.                                                 c == '\r' || c == '\n')
  52.             cp++;
  53.         if (*cp < ' ')
  54.             break;
  55.         if (*cp == '"') {
  56.             cp++;
  57.             while (c = *cp++) {
  58.                 *aptr++ = c;
  59.                 if (c == '"') {
  60.                     if (*cp == '"')
  61.                         cp++;
  62.                     else {
  63.                         aptr[-1] = 0;
  64.                         break;
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.         else {
  70.             while ((c=*cp++) && c != ' ' && c != '\t' && c != '\f' &&
  71.                                                 c != '\r' && c != '\n')
  72.                 *aptr++ = c;
  73.             *aptr++ = 0;
  74.         }
  75.         if (c == 0)
  76.             --cp;
  77.     }
  78.     *aptr = 0;
  79.     if ((_argv = ArpAlloc((long)(_argc+1)*sizeof(*_argv))) == 0) {
  80.         ArpExit(20L, ERROR_NO_FREE_STORE);
  81.     }
  82.     for (c=0,cp=_arg_lin;c<_argc;c++) {
  83.         _argv[c] = cp;
  84.         cp += strlen(cp) + 1;
  85.     }
  86.     _argv[c] = 0;
  87. }
  88.  
  89.